﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class admin_updateBlogroll : System.Web.UI.Page
{
    db_blogroll blogroll = new db_blogroll();
    static DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("inc/checkPurview.aspx");  // 此处用户权限验证

        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("manageBlogroll.aspx");
        }
        if (!Page.IsPostBack)
        {
            string blogrollID = Request.QueryString["id"];
            ds = blogroll.selectBlogrollONE(blogrollID);
            bind_Blogroll(ds);
        }
    }
    public void bind_Blogroll(DataSet ds)
    {
        if (ds.Tables.Count != 0)
        {
            tb_siteAlt.Text = ds.Tables[0].Rows[0]["siteAlt"].ToString();
            tb_siteLink.Text = ds.Tables[0].Rows[0]["sitelink"].ToString();
            tb_siteLogo.Text = ds.Tables[0].Rows[0]["siteLogo"].ToString();
            tb_siteName.Text = ds.Tables[0].Rows[0]["siteName"].ToString();
        }
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        string blogrollID = Request.QueryString["id"];
        string siteAlt = tb_siteAlt.Text;
        string siteLink = tb_siteLink.Text;
        string siteLogo = tb_siteLogo.Text;
        string siteName = tb_siteName.Text;

        if (blogroll.updateBlogroll(blogrollID,siteName, siteLink, siteLogo, siteAlt))
        {
            Response.Write("<script language=javascript>alert('更新友情链接成功！');location.href='manageBlogroll.aspx';</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('失败！');</script>");
        }
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
        bind_Blogroll(ds);
    }
    protected void btn_upload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fud_img.PostedFile.FileName == "")
            {
                Response.Write("<script language=javascript>alert('请选择上传文件！');</script>");
            }
            else
            {
                string filepath = fud_img.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
                if (Tools.IsAllowedExtension(fud_img))
                {
                    if (fud_img.FileContent.Length / 1024 < 200)//检查图片大小
                    {
                        string fileExt = filepath.Substring(filepath.LastIndexOf("."));//.jpg 
                        String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
                        string serverpath = Server.MapPath("~/upload/blogroll/") + newFileName;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg 
                        fud_img.PostedFile.SaveAs(serverpath);//将上传的文件另存为 
                        tb_siteLogo.Text = "/upload/blogroll/" + newFileName;
                        Response.Write("<script language=javascript>alert('上传成功！');</script>");
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('上传图片文件过大，请上传小于200K的图片文件');</script>");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('上传文件格式不正确！必须为：.jpg,.gif,bmp,png格式');</script>");
                }
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('上传发生错误！');</script>");
        }
    }

}